package com.bit.lotterySystem.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.bit.lotterySystem.common.errorcode.ServiceErrorCodeConstants;
import com.bit.lotterySystem.common.exception.ServiceException;
import com.bit.lotterySystem.common.utils.RegexUtil;
import com.bit.lotterySystem.controller.param.UserRegisterParam;
import com.bit.lotterySystem.dao.dataobject.Encrypt;
import com.bit.lotterySystem.dao.dataobject.UserDo;
import com.bit.lotterySystem.dao.mapper.UserMapper;
import com.bit.lotterySystem.service.UserService;
import com.bit.lotterySystem.service.dto.UserRegisterDto;
import com.bit.lotterySystem.service.enums.UserIdentityEnum;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserRegisterDto register(UserRegisterParam param) {
        checkRegisterInfo(param);

        UserDo userDo = new UserDo();
        userDo.setUserName(param.getName());
        userDo.setIdentity(param.getIdentity());
        userDo.setPhoneNumber(new Encrypt(param.getPhoneNumber()));
        userDo.setEmail(param.getMail());
        if(StringUtils.hasText(param.getPassword())) {
            userDo.setPassword(DigestUtil.sha256Hex(param.getPassword()));
        }
        userMapper.insert(userDo);
        UserRegisterDto userRegisterDto = new UserRegisterDto();
        userRegisterDto.setUserId(userDo.getId());
        return userRegisterDto;
    }

    private void checkRegisterInfo(UserRegisterParam param) {
        if(param == null) {
            throw new ServiceException(ServiceErrorCodeConstants.REGISTER_INFO_IS_EMPTY);
        }
        //校验邮箱格式
        if(!RegexUtil.checkMail(param.getMail())){
            throw new ServiceException(ServiceErrorCodeConstants.MAIL_ERROR);
        }
        //校验手机号格式
        if(!RegexUtil.checkMobile(param.getPhoneNumber())) {
            throw new ServiceException(ServiceErrorCodeConstants.PHONENUMBER_ERROR);
        }
        //校验身份信息
        if(null == UserIdentityEnum.forName(param.getIdentity())) {
            throw new ServiceException(ServiceErrorCodeConstants.IDENTITY_ERROR);
        }
        //校验管理员密码，管理员必填
        if(param.getIdentity().equalsIgnoreCase(UserIdentityEnum.ADMIN.name())
        && !StringUtils.hasText(param.getPassword())) {
            throw new ServiceException(ServiceErrorCodeConstants.PASSWORD_IS_EMMPTY);
        }
        //校验密码，至少6位
        if (StringUtils.hasText(param.getPassword())
                && !RegexUtil.checkPassword(param.getPassword())) {
            throw new ServiceException(ServiceErrorCodeConstants.PASSWORD_ERROR);
        }
        //校验邮箱是否被使用
        if(checkMainUsed(param.getMail())){
            throw new ServiceException(ServiceErrorCodeConstants.MAIL_USED);
        }
        //校验手机号是否被使用
        if(checkPhoneNumberUsed(param.getPhoneNumber())) {
            throw new ServiceException(ServiceErrorCodeConstants.PHONENUMBER_USED);
        }
    }

    /**
     * 校验邮箱是否被使用
     * @param mail
     * @return
     */
    private boolean checkMainUsed(@NotBlank(message = "邮箱不能为空") String mail) {
        int count = userMapper.countMail(mail);
        return count > 0;
    }

    /**
     * 校验手机号是否被使用
     * @param phoneNumber
     * @return
     */
    private boolean checkPhoneNumberUsed(String phoneNumber) {
        int count = userMapper.countPhoneNumber(new Encrypt(phoneNumber));
        return count > 0;
    }
}
